//
//  CYView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYView.h"

@interface CYView()
@property (nonatomic, strong) CYView *line;
@property (nonatomic, assign) LineLayout layout;
@end
@implementation CYView

- (instancetype)initWithColor:(UIColor *)color{
    self = [super init];
    if (self) {
        self.backgroundColor = (color)?color:[UIColor whiteColor];
    }
    return self;
}

- (instancetype)initWithColor:(UIColor *)color line:(LineLayout)layout lineColor:(UIColor *)linecolor{
    self = [super init];
    if (self) {
        self.backgroundColor = color;
        _lineWidth = 1000.0;
        _layout = layout;
        self.line.backgroundColor = linecolor;
        [self addLine:layout];
    }
    return self;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:nil];
        [self addSubview:_line];
    }
    
    return _line;
}

- (void)setLineWidth:(CGFloat)lineWidth{
    if (_lineWidth != lineWidth) {
        _lineWidth = lineWidth;
        [self updateLineWidth];
    }
}

- (void)addLine:(LineLayout)layout{
    
    WS(self);
    switch (layout) {
        case LineNormal:
            break;
        case LineTop:
        {
            [_line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(weakSelf).with.offset(0);
                make.left.mas_equalTo(weakSelf).with.offset(0);
                make.right.mas_equalTo(weakSelf.mas_right).with.offset(0);
                make.height.mas_equalTo(MIN(_lineWidth, 1));
            }];
        }
            break;
        case LineLeft:
        {
            [_line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(weakSelf).with.offset(0);
                make.left.mas_equalTo(weakSelf).with.offset(0);
                make.bottom.mas_equalTo(weakSelf.mas_bottom).with.offset(0);
                make.width.mas_equalTo(MIN(_lineWidth, 1));
            }];
        }
            break;
        case LineRight:
        {
            [_line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(weakSelf).with.offset(0);
                make.bottom.mas_equalTo(weakSelf.mas_bottom).with.offset(0);
                make.right.mas_equalTo(weakSelf.mas_right).with.offset(0);
                make.width.mas_equalTo(MIN(_lineWidth, 1));
            }];
        }
            break;
        case LineBottom:
        {
            [_line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(weakSelf.mas_bottom).with.offset(0);
                make.left.mas_equalTo(weakSelf).with.offset(0);
                make.right.mas_equalTo(weakSelf.mas_right).with.offset(0);
                make.height.mas_equalTo(MIN(_lineWidth, 1));
            }];
        }
            break;
        default:
            break;
    }
}

- (void)updateLineWidth{
    
    if (_layout == LineLeft || _layout == LineRight)
        [_line mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(_lineWidth);
        }];
    else if (_layout == LineTop || _layout == LineBottom)
        [_line mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(_lineWidth);
        }];
}

//遍历子视图
- (void)ergodicSubView:(void(^)(CYView *view))view{
    NSArray *array = [self subviews];
    for (int i = 0; i < array.count; i++) {
        view(array[i]);
    }
}

@end
